﻿using System;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data.OleDb;
using System.Data.SqlClient;
using Negocio;

namespace Persistencia
{
    class BrokerDisease : Broker
    {
        public BrokerDisease()
        {
            this.conn = ConnectionDB.getInstance();
        }

        public override void add(Object o)
        {
            Disease diseaseAux = (Disease)o;
            this.conn.connect();
            string sql = "insert into Disease(severityPoints, comment, description, Oid, ofBirth, name, date_, AffectedSystem, status) values('" + diseaseAux.SeverityPoints + "', '" + diseaseAux.Comment + "', '" + diseaseAux.Description + "'," + diseaseAux.OID + "," + diseaseAux.OfBirth + ",'" + diseaseAux.Name + "','" + diseaseAux.Date.ToShortDateString() + "','" + diseaseAux.AffectedSystem + "', '" + diseaseAux.Status + "')";
            string sql1 ="update OID set OId=" + diseaseAux.OID;
            this.conn.execute(sql);
            this.conn.execute(sql1);
            //this.conn.disconnect();
        }

        public override void update(Object o)
        {
            Disease diseaseAux = (Disease)o;
            string sql = "update Disease set severityPoints='"
                + diseaseAux.SeverityPoints + "', comment='" + diseaseAux.Comment
                + "',description = '" + diseaseAux.Description + "',OfBirth = "
                + diseaseAux.OfBirth + ",name = '" + diseaseAux.Name + "',date_ = '"
                + diseaseAux.Date.ToShortDateString() + "',AffectedSystem = '"+ diseaseAux.AffectedSystem 
                + "', status = '" + diseaseAux.Status + "' where oid = " + diseaseAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            //this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            Disease diseaseAux = (Disease)o;
            string sql = "delete Disease where oid=" + diseaseAux.OID;
            //this.conn.connect();
            this.conn.execute(sql);
            //this.conn.disconnect();
        }

        public override void bringData(Object o)
        {
            Disease diseaseAux = (Disease)o;
            this.conn.connect();
            string sql = "Select * from Disease where oid =" + diseaseAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                diseaseAux.SeverityPoints = int.Parse(this.conn.getDataString("severityPoints").ToString());
                diseaseAux.Comment = this.conn.getDataString("comment").ToString().ToString();
                diseaseAux.Description = this.conn.getDataString("description").ToString();
                diseaseAux.OfBirth = (Boolean)this.conn.getDataString("ofBirth");
                diseaseAux.Name = this.conn.getDataString("name").ToString();
                diseaseAux.Date = (DateTime)this.conn.getDataString("Date_");
                diseaseAux.AffectedSystem = this.conn.getDataString("AffectedSystem").ToString();
                diseaseAux.OID = int.Parse(this.conn.getDataString("oid").ToString());
                diseaseAux.Status = this.conn.getDataString("Status").ToString();
            }
            this.conn.disconnect();

        }
        public override ObservableCollection<Object> bringList(Object o)
        {
            string sql = "Select * from Disease order by name asc";
            ObservableCollection<Object> lista = new ObservableCollection<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                Disease diseaseAux = (Disease)o;
                diseaseAux.SeverityPoints = int.Parse(this.conn.getDataString("severityPoints").ToString());
                diseaseAux.Comment = this.conn.getDataString("comment").ToString().ToString();
                diseaseAux.Description = this.conn.getDataString("description").ToString();
                diseaseAux.OfBirth = (Boolean)this.conn.getDataString("ofBirth");
                diseaseAux.Name = this.conn.getDataString("name").ToString();
                diseaseAux.Date = (DateTime)this.conn.getDataString("Date_");
                diseaseAux.AffectedSystem = this.conn.getDataString("AffectedSystem").ToString();
                diseaseAux.Status = this.conn.getDataString("Status").ToString();
                diseaseAux.OID = int.Parse(this.conn.getDataString("oid").ToString());
                lista.Add(diseaseAux);
            }
            this.conn.disconnect();
            return lista;
        }

    }
}
